<template>
    <div class="mod-schedule">
        <el-form :inline="true"
                 :model="dataForm"
                 @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input clearable
                          placeholder="bean名称"
                          v-model="dataForm.beanName"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()">查询</el-button>
                <el-button @click="addOrUpdateHandle()"
                           type="primary"
                           v-if="isAuth('sys:schedule:save')">新增
                </el-button>
                <el-button :disabled="dataListSelections.length <= 0"
                           @click="deleteHandle()"
                           type="danger"
                           v-if="isAuth('sys:schedule:delete')">批量删除
                </el-button>
                <el-button :disabled="dataListSelections.length <= 0"
                           @click="pauseHandle()"
                           type="danger"
                           v-if="isAuth('sys:schedule:pause')">批量暂停
                </el-button>
                <el-button :disabled="dataListSelections.length <= 0"
                           @click="resumeHandle()"
                           type="danger"
                           v-if="isAuth('sys:schedule:resume')">批量恢复
                </el-button>
                <el-button :disabled="dataListSelections.length <= 0"
                           @click="runHandle()"
                           type="danger"
                           v-if="isAuth('sys:schedule:run')">批量立即执行
                </el-button>
                <el-button @click="logHandle()"
                           type="success"
                           v-if="isAuth('sys:schedule:log')">日志列表
                </el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList"
                  @selection-change="selectionChangeHandle"
                  border
                  style="width: 100%;"
                  v-loading="dataListLoading">
            <el-table-column align="center"
                             header-align="center"
                             type="selection"
                             width="50">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="ID"
                             prop="jobId"
                             width="80">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="bean名称"
                             prop="beanName">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="方法名称"
                             prop="methodName">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="参数"
                             prop="params">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="cron表达式"
                             prop="cronExpression">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="备注"
                             prop="remark">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="状态"
                             prop="status">
                <template slot-scope="scope">
                    <el-tag size="small"
                            v-if="scope.row.status === 0">正常
                    </el-tag>
                    <el-tag size="small"
                            type="danger"
                            v-else>暂停
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column align="center"
                             fixed="right"
                             header-align="center"
                             label="操作"
                             width="150">
                <template slot-scope="scope">
                    <el-button @click="addOrUpdateHandle(scope.row.jobId)"
                               size="small"
                               type="text"
                               v-if="isAuth('sys:schedule:update')">修改
                    </el-button>
                    <el-button @click="deleteHandle(scope.row.jobId)"
                               size="small"
                               type="text"
                               v-if="isAuth('sys:schedule:delete')">删除
                    </el-button>
                    <el-button @click="pauseHandle(scope.row.jobId)"
                               size="small"
                               type="text"
                               v-if="isAuth('sys:schedule:pause')">暂停
                    </el-button>
                    <el-button @click="resumeHandle(scope.row.jobId)"
                               size="small"
                               type="text"
                               v-if="isAuth('sys:schedule:resume')">恢复
                    </el-button>
                    <el-button @click="runHandle(scope.row.jobId)"
                               size="small"
                               type="text"
                               v-if="isAuth('sys:schedule:run')">立即执行
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination :current-page="pageIndex"
                       :page-size="pageSize"
                       :page-sizes="[10, 20, 50, 100]"
                       :total="totalPage"
                       @current-change="currentChangeHandle"
                       @size-change="sizeChangeHandle"
                       layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update @refreshDataList="getDataList"
                       ref="addOrUpdate"
                       v-if="addOrUpdateVisible"></add-or-update>
        <!-- 弹窗, 日志列表 -->
        <log ref="log"
             v-if="logVisible"></log>
    </div>
</template>

<script>
    import AddOrUpdate from './schedule-add-or-update'
    import Log from './schedule-log'

    export default {
        data() {
            return {
                dataForm: {
                    beanName: ''
                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,

                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false,
                logVisible: false
            }
        },
        components: {
            AddOrUpdate,
            Log
        },
        activated() {
            this.getDataList()
        },
        methods: {
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true
                this.$http({
                    url: this.$http.adornUrl('/sys/schedule/page'),
                    method: 'get',
                    params: this.$http.adornParams({
                        'page': this.pageIndex,
                        'limit': this.pageSize,
                        'beanName': this.dataForm.beanName
                    })
                }).then(({data}) => {
                    this.dataList = data.records
                    this.totalPage = data.total
                    this.dataListLoading = false
                })
            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 多选
            selectionChangeHandle(val) {
                this.dataListSelections = val
            },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.jobId
                })
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/sys/schedule'),
                        method: 'delete',
                        data: this.$http.adornData(ids, false)
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    })
                }).catch(() => {
                })
            },
            // 暂停
            pauseHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.jobId
                })
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '暂停' : '批量暂停'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/sys/schedule/pause'),
                        method: 'post',
                        data: this.$http.adornData(ids, false)
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    })
                }).catch(() => {
                })
            },
            // 恢复
            resumeHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.jobId
                })
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '恢复' : '批量恢复'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/sys/schedule/resume'),
                        method: 'post',
                        data: this.$http.adornData(ids, false)
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    })
                }).catch(() => {
                })
            },
            // 立即执行
            runHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.jobId
                })
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '立即执行' : '批量立即执行'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/sys/schedule/run'),
                        method: 'post',
                        data: this.$http.adornData(ids, false)
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    })
                }).catch(() => {
                })
            },
            // 日志列表
            logHandle() {
                this.logVisible = true
                this.$nextTick(() => {
                    this.$refs.log.init()
                })
            }
        }
    }
</script>
